import { inject, onUnmounted } from "vue";

export interface ToggleItemCustomHooks {
  onShow: (fn: () => void) => void;
  onShowOnce: (fn: () => void) => void;
  removeShowHandler: (fn: () => void) => void;
  onHide: (fn: () => void) => void;
  onHideOnce: (fn: () => void) => void;
  removeHideHandler: (fn: () => void) => void;
}

export function useOnShow(fn: () => void) {
  const util = inject<ToggleItemCustomHooks>("toggle-item-custom-hooks");
  if (util == null) {
    throw new Error("toggle-item-custom-hooks is not defined");
  }
  util.onShow(fn);
  onUnmounted(() => util.removeShowHandler(fn));
}

export function useOnShowOnce(fn: () => void) {
  const util = inject<ToggleItemCustomHooks>("toggle-item-custom-hooks");
  if (util == null) {
    throw new Error("toggle-item-custom-hooks is not defined");
  }
  util.onShowOnce(fn);
  onUnmounted(() => util.removeShowHandler(fn));
}

export function useOnHide(fn: () => void) {
  const util = inject<ToggleItemCustomHooks>("toggle-item-custom-hooks");
  if (util == null) {
    throw new Error("toggle-item-custom-hooks is not defined");
  }
  util.onHide(fn);
  onUnmounted(() => util.removeShowHandler(fn));
}

export function useOnHideOnce(fn: () => void) {
  const util = inject<ToggleItemCustomHooks>("toggle-item-custom-hooks");
  if (util == null) {
    throw new Error("toggle-item-custom-hooks is not defined");
  }
  util.onHideOnce(fn);
  onUnmounted(() => util.removeShowHandler(fn));
}
